<?php
namespace app\admin\controller;
use think\Db;
use app\admin\controller\Admin;
use think\Controller;
use think\Request;
class Admininfo extends Admin
{	
	

	public function __construct(){
		parent::initialize();
		// var_dump($this->initialize()) ;

	}

    public function admininfo()
    {

    	$list = Db::name('admininfo')->field(['adminname','power','id'])->select();

        $data = Db::name('admininfo')
        		->alias('a')
			    ->join('tb_admin_role ar','a.id = ar.uid')
	            ->join('tb_role r', 'ar.rid = r.id')
        		->field(['adminname', 'power','a.id as aid','role'])->select();


        // var_dump($data,$list);	
        foreach ($list as $k => $v) {
	        $data = Db::name('tb_role')
	        		->alias('r')
		          ->join('tb_admin_role ar', 'ar.rid = r.id')
		          ->where('ar.uid',$v['id'])
	        		->field([ 'role'])->select();
	        $arr[$v['id']]=$data;
        }
       	// var_dump($arr);
       	

        return $this->fetch('admininfo/admininfo',[
            'list' => $list,
            'arr'  => $arr,
        ]);

        //获取模块下的控制器

    }

    //显示编辑页面
    public function edit($id)
    {   
        
        $res = Db::name('admininfo')->where('id',$id)->field(['adminname', 'id'])->select();

//        var_dump($res);
      
        $data = Db::name('tb_admin_role')->where('uid',$id)->field(['rid'])->select();
        foreach ($data as $k => $v) {
           $dat[] = $v['rid'] ;
        }

        if ($data==null) {
            $dat[]=0;
        }
        
        $list = Db::name('tb_role')->field(['id', 'role'])->select();
        return $this->fetch('admininfo/edit',[
                    'list'=>$list,
                    'res'=>$res,
                    'dat'=>$dat
            ]);

    }

    public function update(Request $request)
    {
    	$data = Request::instance()->post();
    	
    	$list = Db::name('tb_admin_role')->where('uid',$data['uid'])->delete();
    	
    	foreach ($data as $k => $v) {

    	    if ($k!='name'&&$k!='uid') {
    	         $list = Db::name('tb_admin_role')->data(['uid'=>$data['uid'],'rid'=>$v])->insert();                
    	    }
    	}

    	if ($list) {
    	   return $this->success('更改成功',url('admin/admininfo/admininfo'));
    	}else{
    		return $this->error('更改失败');
    	}
    }
    public function addshow()
    {
        $list = Db::name('tb_role')->field(['id','role'])->select();
    	return $this->fetch('admininfo/addadmininfo',[
    			'list'=>$list
    		]);
    }


    public function add(Request $request)
    {	
    	$data = Request::instance()->post();
    	// var_dump($data);die;
      
         // if(preg_match('/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,18}$/',$data['pass'])){ //匹配
         //     return $this->error('密码必须由6-18位字母数字混合组成');
         // } 
         $res = Db::name('admininfo')->where('adminname',$data['name'])->select();
         if(!empty($res)){
            return $this->error('数据库已有此人');
         }

    	if (!empty($data['name'])) {
    		if (!empty($data['pass'])) {
    			if ($data['pass'] == $data['repass']) {
      				$list = Db::name('admininfo')->insertGetid([
      					'adminname'=>$data['name'],
      					'password'=>$data['pass'],
      					]);
      				if (!empty($data['arr'])) {
      					
	      				foreach ($data['arr'] as $k => $v) {
	      					
	      				$data = Db::name('tb_admin_role')->data(['uid'=>$list,'rid'=>$v])->insert();
	      				}
      				}
      				if ($data) {

      					return $this->success('增加成功',url('admin/Admininfo/admininfo'));
      				}else{
      					return $this->error('添加失败');
      				}
    				
    			}else{
    				return $this->error('两次密码不一样');
    			}
    		}else{
    			return $this->erroe('密码不能为空');
    		}
    	}else{
    		return $this->error('名字不能为空');
    	}
    	
    }


   public function delete(Request $request)
   {
       $data = Request::instance()->post();
       $res = Db::name('Admininfo')->where('id',$data['id'])->delete();
       if ($res) {
           
           $list = Db::name('tb_admin_role')->where('uid',$data['id'])->delete();
       }

       if ($res) {
           return $res;         
       }

   }

}
